#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""

@author ：chenzf
@date ：2024/4/24 上午11:26 
"""
import re

str = "python it python it python it python it"
result = re.match("python", str)
print(result)
print(result.group())
print(result.start())
print(result.end())
print(result.span())
# 从开头匹配第一个python
str = "1python it python it python it python it"
result = re.match("python", str)
print(f"匹配结果:{result}")
str = "pythonitpythoniython"
# 搜索
search_result = re.search('python', str)
print(f"搜索结果:{search_result}")
# 匹配所有
all_result = re.findall('python', str)
print(f"匹配所有结果:{all_result}")
#正则匹配
str = "python2it3!!!python@#@$i&ython"
reg_result = re.findall(r"\W", str)
print(f"匹配所有结果1:{reg_result}")
reg_result = re.findall(r"[a-zA-Z]", str)
print(f"匹配所有结果2:{reg_result}")
reg_result = re.findall(r"[a-zA-Z]{2,5}", str)
print(f"匹配所有结果3:{reg_result}")
#匹配账号。长度在6-10
str = "123asnn"
print(re.findall(r"[0-9a-zA-Z]{6,10}",str))
# 匹配QQ号长度5-11 第一个字符不为0
str = "123456"
# str = "0123456"
findall_result = re.findall(r"^[1-9][0-9]{5,11}$", str)
print(f"匹配QQ:{findall_result}")
# 匹配邮箱地址 只允许qq,163,gmail
mail = "123456@qq.com"
match_result = re.match(r"(^[\w-]+(\.[\w-]+)*@(qq|163|gmail)(\.[\w-]+)+$)", mail)
findall_result = re.findall(r"(^[\w-]+(\.[\w-]+)*@(qq|163|gmail)(\.[\w-]+)+$)", mail)
print(f"匹配邮箱：{match_result.string}")
print(f"匹配邮箱：{findall_result}")